package com.spring.sunflower.dialog

import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import com.blankj.utilcode.util.SPUtils
import com.lxj.xpopup.core.CenterPopupView
import com.spring.sunflower.R
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.AppUtils
import kotlinx.android.synthetic.main.custom_bottom_dialog.view.*

/**
 * Created by Kevin on 2021/8/6<br/>
 *
 * Describe:<br/>
 */
class TipsIntroDialog(
    context: Context,
    var listener: OnPermissionIntroClickListener?
) : CenterPopupView(context) {
    private var llContainer: LinearLayout? = null
    private var tvOK: TextView? = null

    //    private var tvCancel: TextView? = null
    private var tv: TextView? = null

    //    private var checkbox:CheckBox?=null
//    private var tvTitle: TextView? = null
    private var tvDesc: TextView? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_tip_intro
    }

    override fun onCreate() {
        super.onCreate()
        llContainer = findViewById<LinearLayout>(R.id.llContainer)
        tvOK = findViewById(R.id.tvOK)
        tv = findViewById(R.id.tv)
        tvDesc = findViewById<TextView>(R.id.tvDesc)
        val checkbox = findViewById<CheckBox>(R.id.checkBox)
        xListener?.onCreateView()
        tvOK?.setOnClickListener {
            if (checkbox.isChecked) {
                SPUtils.getInstance().put(Constant.KEY_SHOW_VIDEO_RULE_DIALOG, true)
            }
            listener?.onClickOK()
            dismiss()
        }
//        val formatString =
//            AppUtils.formatString(context.getString(R.string.video_tip), Color.RED, "封号处理")
        val string = context.getString(R.string.video_tip)
        val index1 = string.indexOf("禁止涉黄")
        val index2 = string.indexOf("封号处理")

        val ss = SpannableString(string)
        ss.setSpan(
            ForegroundColorSpan(AppUtils.getColorX(context, R.color.colorPrimary)),
            index1,
            index1 + 4,
            Spanned.SPAN_EXCLUSIVE_INCLUSIVE
        )
        ss.setSpan(
            ForegroundColorSpan(AppUtils.getColorX(context, R.color.colorPrimary)),
            index2,
            index2 + 4,
            Spanned.SPAN_EXCLUSIVE_INCLUSIVE
        )
        tvDesc?.setText(ss)
    }

    fun getContainer(): LinearLayout {
        return llContainer!!
    }

    private var xListener: OnCreateViewListener? = null
    fun setOnCreateViewListener(l: OnCreateViewListener) {
        xListener = l
    }

    fun interface OnCreateViewListener {
        fun onCreateView()
    }

    fun setOKText(str: String) {
        if (str.isNullOrEmpty()) return
        tvOK?.text = str
    }

    fun setCancelText(str: String) {
        if (str.isNullOrEmpty()) return
        tvCancel?.text = str
    }

    fun setHeadTitle(str: String) {
        if (str.isNullOrEmpty()) return
        tv?.text = str
    }


    fun setDescText(str: String) {
        if (str.isNullOrEmpty()) return
        tvDesc?.text = str
    }

    fun setDescColor(@ColorInt color: Int) {
        tvDesc?.setTextColor(color)
    }
}